﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CompanyBussiness;

namespace Website.admin.Control
{
    public partial class cTypeProduct : System.Web.UI.UserControl
    {
        ProductManagement proMana = new ProductManagement();
        LanguagesManagement languageMana = new LanguagesManagement();
        CompanyBussiness.Language language;

        protected void Page_Load(object sender, EventArgs e)
        {
            fail.Visible = false;


            if (Request.Cookies[CONST.COOKIES_LANGUAGE] == null)
            {
                Session[CONST.SESSION_LANGUAGE] = "";
                language = languageMana.GetLanguageByCode("vi-VN");
            }
            else
            {
                Session[CONST.SESSION_LANGUAGE] = Request.Cookies[CONST.COOKIES_LANGUAGE][CONST.COOKIES_LANGUAGE_ID];
                language = languageMana.GetLanguageByID(int.Parse(Session[CONST.SESSION_LANGUAGE].ToString()));
            }
            if (!Page.IsPostBack)
            {
                this.LoadTreeView();
            }
        }

        protected void LoadTreeView()
        {
            try
            {
                List<TypeProduct> listParent = proMana.GetParentTypeProductAll(language.LanguagesID);
                foreach (var pTypeProduct in listParent)
                {
                    TreeNode root = new TreeNode();
                    root.Text = pTypeProduct.TypeProductName.ToString();
                    root.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=ParentTypeProductDetail&" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + pTypeProduct.TypeProductID.ToString();
                    root.ToolTip = "Click để cập nhật cho danh mục này";
                    List<TypeProduct> listChild = proMana.GetChildrenTypeProductByParentIDAll((int)pTypeProduct.TypeProductID, (int)language.LanguagesID);
                    foreach (var cTypeProduct in listChild)
                    {
                        TreeNode child = new TreeNode();
                        child.Text = cTypeProduct.TypeProductName.ToString();
                        child.NavigateUrl = Request.CurrentExecutionFilePath + "?" + CONST.ACTION + "=ParentTypeProductDetail&" + CONST.QUERYSTRING_TYPE_PRODUCT + "=" + cTypeProduct.TypeProductID.ToString();
                        child.ToolTip = "Click để cập nhật cho danh mục này";
                        root.ChildNodes.Add(child);
                    }
                    TreeViewTypeProduct.Nodes.Add(root);
                }

                TreeViewTypeProduct.CollapseAll();
            }
            catch (Exception)
            {
                fail.Visible = true;
                lbError.Text = "Có lỗi trong khi Load dữ liệu . Bạn hãy thử lại.";
            }
        }

        protected void TreeViewTypeProduct_SelectedNodeChanged(object sender, EventArgs e)
        {

        }

        protected void btnAddTypeProduct_Click(object sender, EventArgs e)
        {
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "AddNewTypeProduct";
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_TYPE] = "AddTypeProduct";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=AddNewTypeProduct");
        }
    }
}